package com.shuai.jit.security.config;


import com.shuai.jit.security.filter.MyBasicAuthenticationFilter;
import com.shuai.jit.security.filter.MyUsernamePasswordFilter;
import com.shuai.jit.security.security.DefaultPasswordEncoder;
import com.shuai.jit.security.security.TokenLogoutHandler;
import com.shuai.jit.security.security.UnauthorizedEntryPoint;
import com.shuai.jit.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * SpringSecurity核心配置类
 * @author bn
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {

    private UserDetailsService userDetailsService;
    private JwtUtil tokenManager;
    private DefaultPasswordEncoder defaultPasswordEncoder;

    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    public TokenWebSecurityConfig(UserDetailsService jwtUserDetailsService, DefaultPasswordEncoder defaultPasswordEncoder,
                                  JwtUtil tokenManager, RedisTemplate<String,Object> redisTemplate) {
        this.userDetailsService = jwtUserDetailsService;
        this.defaultPasswordEncoder = defaultPasswordEncoder;
        this.tokenManager = tokenManager;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 配置设置
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(new UnauthorizedEntryPoint())
                .and().csrf().disable()
                .authorizeRequests()
                .anyRequest().authenticated()
                // 和注册界面
                .and().logout().logoutUrl("/logout")
                .addLogoutHandler(new TokenLogoutHandler(tokenManager,redisTemplate)).and()
                // 添加登录系统
                .addFilter(new MyUsernamePasswordFilter(authenticationManager(), tokenManager, redisTemplate))
                // 添加登出系统
                .addFilter(new MyBasicAuthenticationFilter(authenticationManager(), tokenManager, redisTemplate)).httpBasic();
    }

    /**
     * 密码处理
     * @param auth
     * @throws Exception
     */
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(defaultPasswordEncoder);
    }

    /**
     * 配置哪些请求不拦截
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers("/favicon.ico")
                .antMatchers("/swagger-resources/**")
                .antMatchers("/swagger-ui.html/**")
                .antMatchers("/swagger/**")
                .antMatchers("/v3/**")
                .antMatchers( "/webjars/**")
                .antMatchers("/api/**")
                .antMatchers("/user/send")
                .antMatchers("/user/telephoneCode")
                .antMatchers("/user/numberPassword")
                .antMatchers("/user/register")
                .antMatchers("/myChat/**")
                .antMatchers("/upload")
                .antMatchers("/user/**")
                .antMatchers("/article/**")
                .antMatchers("/comment/**")
                .antMatchers("/moment/**")
                .antMatchers("/queueUser/**")
                .antMatchers("/app/**");
    }
}
